package fr.enseirb.mycub.structures.bus;

import java.util.ArrayList;

import android.database.Cursor;
import android.provider.BaseColumns;
import android.util.Log;

import com.google.android.maps.GeoPoint;

import fr.enseirb.mycub.db.BusDBConstants;
import fr.enseirb.mycub.structures.Line;
import fr.enseirb.mycub.structures.SpecificLine;
import fr.enseirb.mycub.structures.Station;

/**
 * Model class which will represent a Bus station
 * 
 * @author jletemple, coudar
 * 
 */

public class BusStation extends Station {
	private ArrayList<SpecificLine> line;

	public static final String LINE = "lineNumber";

	public BusStation(int id, String name,
			int longitude, int latitude, ArrayList<SpecificLine> line,
			boolean isOpen, boolean isFavorite, int distance) {
		super();
		super.mId = id;
		super.mName = name;
		setGeoPoint(new GeoPoint(latitude, longitude));
		this.setLine(line);
		super.mIsOpen = isOpen;
		super.mIsFavorite = isFavorite;
		super.mDistance = distance;
	}

	public BusStation(){}

	public ArrayList<SpecificLine> getLine() {
		return line;
	}

	public void setLine(ArrayList<SpecificLine> line) {
		this.line = line;
	}

	@Override
	public String toString() {
		return "BusStation [line=" + line + ", mId=" + mId + ", mName="
				+ mName + ", mGeoPoint=" + mGeoPoint + ", mDistance="
				+ mDistance + ", mIsOpen=" + mIsOpen + ", mIsFavorite="
				+ mIsFavorite + "]";
	}

	public static ArrayList<BusStation> getSimpleStationFromCursor(ArrayList<SpecificLine> specificLines,
			Cursor stationsCursor, Cursor linkTableCursor){

		ArrayList<BusStation> stations = new ArrayList<BusStation>();

		if(stationsCursor.getCount() == 0 ) return stations;

		//------- stations table -------

		int latitudeColumn = stationsCursor
				.getColumnIndex(BusDBConstants.KEY_LATITUDE);
		int longitudeColumn = stationsCursor
				.getColumnIndex(BusDBConstants.KEY_LONGITUDE);
		int idColumn = stationsCursor
				.getColumnIndex(BaseColumns._ID);

		stationsCursor.moveToFirst();
		BusStation l = null;

		int size = stationsCursor.getCount();
		//size = Math.min(size, 100);
		for(int i =  0; i < size; i ++){
			l = new BusStation();
			l.mId = stationsCursor.getInt(idColumn);
			int lat = stationsCursor.getInt(latitudeColumn);
			int lon = stationsCursor.getInt(longitudeColumn);
			l.mGeoPoint = new GeoPoint(lat,lon);

		//Log.i("TS" , "--> nb de lignes : "+l.getLine().size()+" pour la station : "+l.mId);
		stations.add(l);
		stationsCursor.moveToNext();
	}

	stationsCursor.close();

	return stations;
}



}